﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PowershellHostUI.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Implementation of PSHostUserInterface.
//   Input methods are not implemented.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Powershell
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Management.Automation;
    using System.Management.Automation.Host;
    using System.Security;

    using Tracing;

    using Utils;

    /// <summary>
    /// Implementation of PSHostUserInterface.
    /// Input methods are not implemented.
    /// </summary>
    public class PowershellHostUI : PSHostUserInterface
    {
        private readonly IPowershellAction action;
        private readonly PowershellHostRawUI rawUI;

        private TraceSwitch TraceSwitch
        {
            get
            {
                return ActionsTraceHelper.TraceSwitch;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PowershellHostUI"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        public PowershellHostUI(IPowershellAction action)
        {
            this.action = action;
            this.rawUI = new PowershellHostRawUI();
        }

        /// <summary>
        /// Prompts the specified caption.
        /// </summary>
        /// <param name="caption">The caption.</param>
        /// <param name="message">The message.</param>
        /// <param name="descriptions">The descriptions.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public override Dictionary<string, PSObject> Prompt(
            string caption, 
            string message, 
            Collection<FieldDescription> descriptions)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Prompts for choice.
        /// </summary>
        /// <param name="caption">The caption.</param>
        /// <param name="message">The message.</param>
        /// <param name="choices">The choices.</param>
        /// <param name="defaultChoice">The default choice.</param>
        /// <returns></returns>
        public override int PromptForChoice(
            string caption, 
            string message, 
            Collection<ChoiceDescription> choices, 
            int defaultChoice)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Prompts for credential.
        /// </summary>
        /// <param name="caption">The caption.</param>
        /// <param name="message">The message.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="targetName">Name of the target.</param>
        /// <param name="allowedCredentialTypes">The allowed credential types.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public override PSCredential PromptForCredential(
            string caption,
            string message,
            string userName,
            string targetName,
            PSCredentialTypes allowedCredentialTypes,
            PSCredentialUIOptions options)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Prompts for credential.
        /// </summary>
        /// <param name="caption">The caption.</param>
        /// <param name="message">The message.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="targetName">Name of the target.</param>
        /// <returns></returns>
        public override PSCredential PromptForCredential(
            string caption, 
            string message, 
            string userName, 
            string targetName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the raw UI.
        /// </summary>
        /// <value>The raw UI.</value>
        public override PSHostRawUserInterface RawUI
        {
            get { return this.rawUI; }
        }

        /// <summary>
        /// Reads the line.
        /// </summary>
        /// <returns></returns>
        public override string ReadLine()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Reads the line as secure string.
        /// </summary>
        /// <returns></returns>
        public override SecureString ReadLineAsSecureString()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Writes the specified foreground color.
        /// </summary>
        /// <param name="foregroundColor">Color of the foreground.</param>
        /// <param name="backgroundColor">Color of the background.</param>
        /// <param name="value">The value.</param>
        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            TraceHelper.TraceInfo(this.TraceSwitch, "PowershellHostUI", value);
        }

        /// <summary>
        /// Writes the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        public override void Write(string value)
        {
            TraceHelper.TraceInfo(this.TraceSwitch, "PowershellHostUI", value);
        }

        /// <summary>
        /// Writes the debug line.
        /// </summary>
        /// <param name="message">The message.</param>
        public override void WriteDebugLine(string message)
        {
            TraceHelper.TraceVerbose(this.TraceSwitch, "PowershellHostUI", message);
        }

        /// <summary>
        /// Writes the error line.
        /// </summary>
        /// <param name="value">The value.</param>
        public override void WriteErrorLine(string value)
        {
            TraceHelper.TraceWarning(this.TraceSwitch, "PowershellHostUI", value);
        }

        /// <summary>
        /// Writes the line.
        /// </summary>
        /// <param name="value">The value.</param>
        public override void WriteLine(string value)
        {
            TraceHelper.TraceInfo(this.TraceSwitch, "PowershellHostUI", value);
        }

        /// <summary>
        /// Writes the progress.
        /// </summary>
        /// <param name="sourceId">The source id.</param>
        /// <param name="record">The record.</param>
        public override void WriteProgress(long sourceId, System.Management.Automation.ProgressRecord record)
        {
            TraceHelper.TraceVerbose(
                this.TraceSwitch,
                "PowershellHostUI",
                "ProgressUpdate (Activity, ActivityId, CurrentOperation, ParentActivityId, PercentComplete, SecondsRemaining, StatusDescription)",
                record.Activity,
                record.ActivityId,
                record.CurrentOperation,
                record.ParentActivityId,
                record.PercentComplete,
                record.SecondsRemaining,
                record.StatusDescription);
        }

        /// <summary>
        /// Writes the verbose line.
        /// </summary>
        /// <param name="message">The message.</param>
        public override void WriteVerboseLine(string message)
        {
            TraceHelper.TraceVerbose(this.TraceSwitch, "PowershellHostUI", message);
        }

        /// <summary>
        /// Writes the warning line.
        /// </summary>
        /// <param name="message">The message.</param>
        public override void WriteWarningLine(string message)
        {
            TraceHelper.TraceWarning(this.TraceSwitch, "PowershellHostUI", message);
        }

    }
}